package com.luhuiguo.cms.server.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.entity.Channel;
import com.luhuiguo.cms.server.service.ChannelService;
import com.luhuiguo.cms.server.service.MapperService;
import com.luhuiguo.cms.shared.model.ChannelModel;
import com.luhuiguo.cms.shared.rpc.FetchChannels;
import com.luhuiguo.cms.shared.rpc.FetchChannelsResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class FetchChannelsHandler implements ActionHandler<FetchChannels, FetchChannelsResult> {

    @Autowired
    private MapperService mapperService;

    @Autowired
    private ChannelService channelService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public FetchChannelsResult execute(FetchChannels action, ExecutionContext context) throws ActionException {

        List<Channel> list = channelService.findChannels();

        return new FetchChannelsResult(mapperService.mapList(list, ChannelModel.class));

    }

    @Override
    public Class<FetchChannels> getActionType() {
        return FetchChannels.class;
    }

    @Override
    public void rollback(FetchChannels action, FetchChannelsResult result, ExecutionContext context)
            throws ActionException {

    }

}
